package main

import (
	"expvar"
	"log"
	"net/http"
	"time"
)

var upTimeMetrics *upTimeVar

type upTimeVar struct {
	value time.Time
}

func (v *upTimeVar) Set(date time.Time) {
	v.value = date
}

func (v *upTimeVar) Add(duration time.Duration) {
	v.value = v.value.Add(duration)
}

func (v *upTimeVar) String() string {
	return v.value.Format(time.UnixDate)
}

func init() {
	upTimeMetrics = &upTimeVar{value: time.Now().Local()}
	expvar.Publish("uptime", upTimeMetrics)
}

func main() {
	// 使用默认的ServeMux，在端口6060上启动一个HTTP服务器
	err := http.ListenAndServe(":6060", http.DefaultServeMux)
	if err != nil {
		log.Fatalf("http.ListenAndServe err: %v", err)
	}
}
